package org.gntc.prayer.database;

import java.sql.Date;

import org.gntc.prayer.R;
import org.gntc.prayer.database.PrayerDBInfo.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class PrayerDB {
	private static final String DATABASE_NAME = "prayer.db";
	private static final int DATABASE_VERSION = 1;
	private SQLiteDatabase mDB;
	private PryaerDatabaseHelper mDBHelper;
	private Context mCtx;
	
	private class PryaerDatabaseHelper extends SQLiteOpenHelper{
		
		private static final String FOREIGNKEY_ON_CMD = "PRAGMA foreign_keys = ON;";

		public PryaerDatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(FOREIGNKEY_ON_CMD);
			db.execSQL(PrayerTable._CREATE);
			db.execSQL(PrayerCategoryTable._CREATE);
			
			mDB = db;
			initCategoryName();
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL(PrayerCategoryTable._DROP);
			db.execSQL(PrayerTable._DROP);
			onCreate(db);
		}
		
		private void initCategoryName(){
			String[] category_name = mCtx.getResources().getStringArray(R.array.category_name);
			for (String name : category_name) {
				insertCategory(name);
			}
		}
	}
	
	public PrayerDB(Context context){
		this.mCtx = context;
	}
	
	public PrayerDB open() throws SQLiteException{
		mDBHelper = new PryaerDatabaseHelper(mCtx, DATABASE_NAME, null, DATABASE_VERSION);
		mDB = mDBHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		mDB.close();
	}
	
	public long insertCategory(String category_name) throws SQLiteException{
		ContentValues values = new ContentValues();
		values.put(PrayerCategoryTable.NAME, category_name);
		
		return mDB.insertOrThrow(PrayerCategoryTable._TABLENAME, null, values);
	}
	
	public long insertPrayer(String title, String content, Date registered, Date start_date, Date end_date, int category, boolean favorite) throws SQLiteException {
		String regist = null;
		String start = null;
		String end = null;
		if (registered != null) {
			regist = registered.toString();
		}
		
		if (start_date != null) {
			start = start_date.toString();
		}
		
		if (end_date != null) {
			end = end_date.toString();
		}
		
		ContentValues values = new ContentValues();
		values.put(PrayerTable.TITLE, title);
		values.put(PrayerTable.CONTENT, content);
		values.put(PrayerTable.REGISTERED_DATE, regist);
		values.put(PrayerTable.START_DATE, start);
		values.put(PrayerTable.END_DATE, end);
		values.put(PrayerTable.CATEGORY, category);
		values.put(PrayerTable.FAVORITE, favorite);
		
		return mDB.insertOrThrow(PrayerTable._TABLENAME, null, values);
	}
	
	public Cursor getAllCategoryName(){
		Cursor cursor = mDB.query(PrayerCategoryTable._TABLENAME, null, null, null, null, null, null);

		if(cursor!=null && cursor.getCount() != 0){
			cursor.moveToFirst();
		}
		
		return cursor;
	}
	
	public Cursor getAllPrayer(){
		Cursor cursor = mDB.query(PrayerTable._TABLENAME, null, null, null, null, null, null);
		
		if(cursor!=null && cursor.getCount() != 0){
			cursor.moveToFirst();
		}
		
		return cursor;
	}
}